package com.hc.bank.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

public class CityServlet extends HttpServlet {

	private static final long serialVersionUID = 3382382545066071923L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int id = Integer.valueOf(request.getParameter("id"));
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		System.out.println("获得城市：--------------------" + id);

		PrintWriter out = response.getWriter();

		if (id == 1) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", 1001);
			map.put("name", "长沙");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("id", 1002);
			map.put("name", "株洲");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("id", 1003);
			map.put("name", "湘潭");
			list.add(map);
		} else if (id == 2) {

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", 2001);
			map.put("name", "武汉");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("id", 2002);
			map.put("name", "汉阳");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("id", 2003);
			map.put("name", "襄阳");
			list.add(map);
		} else if (id == 3) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", 3001);
			map.put("name", "深圳");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("id", 3002);
			map.put("name", "广州");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("id", 3003);
			map.put("name", "梅州");
			list.add(map);
		} else if (id == 4) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", 4001);
			map.put("name", "南宁");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("id", 4002);
			map.put("name", "玉林");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("id", 4003);
			map.put("name", "北海");
			list.add(map);
		}

		String json = JSON.toJSONString(list);
		out.println(json);
		out.flush();
	}
}
